package com.rocsea.robot.controller;

import com.rocsea.robot.model.request.KettleBuildRequest;
import com.rocsea.robot.model.request.KettleExportRequest;
import com.rocsea.robot.model.response.ResponseWrapper;
import com.rocsea.robot.service.KettleRobotService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author RocSea
 * @Date 2022/7/11
 */
@RestController
@RequestMapping("build")
public class KettleRobotController {
    @Resource
    private KettleRobotService kettleRobotService;

    @ApiOperation(value = "半自动化")
    @PostMapping(value = "half/auto")
    public ResponseWrapper halfAuto(@RequestBody KettleBuildRequest request) {
        try {
            kettleRobotService.autoBuild(request);
            return ResponseWrapper.success("操作成功");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseWrapper.fail(-1, ex.getMessage());
        }
    }

    @ApiOperation(value = "全自动化")
    @PostMapping(value = "full/auto")
    public ResponseWrapper fullAuto(@RequestParam(value = "range", required = false) String range,
                                    @RequestParam("file") MultipartFile file) {
        try {
            kettleRobotService.fullAuto(file, range);
            return ResponseWrapper.success("操作成功");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseWrapper.fail(-1, ex.getMessage());
        }
    }

    @ApiOperation(value = "全量删除转换/作业")
    @DeleteMapping(value = "delete")
    @ResponseBody
    public String buildDel(@ApiParam("目录ID") @RequestParam Long idDirectory) {
        return kettleRobotService.buildDel(idDirectory);
    }

    @ApiOperation(value = "全量导出包含目录、转换、job")
    @PostMapping(value = "export")
    public void export(@RequestBody KettleExportRequest request,
                       HttpServletResponse response) throws Exception {
        kettleRobotService.export(request, response);
    }
}
